import plotly.express as px
import seaborn as sb
sb.set(style = "white")
import plotly.graph_objects as gpx
import plotly.io as pio
import kaleido
df = px.data.iris()
df
| sepal_length | sepal_width | petal_length | petal_width | species | species_id | |
|---|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa | 1 |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa | 1 |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa | 1 |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa | 1 |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa | 1 |
| ... | ... | ... | ... | ... | ... | ... |
| 145 | 6.7 | 3.0 | 5.2 | 2.3 | virginica | 3 |
| 146 | 6.3 | 2.5 | 5.0 | 1.9 | virginica | 3 |
| 147 | 6.5 | 3.0 | 5.2 | 2.0 | virginica | 3 |
| 148 | 6.2 | 3.4 | 5.4 | 2.3 | virginica | 3 |
| 149 | 5.9 | 3.0 | 5.1 | 1.8 | virginica | 3 |
150 rows × 6 columns
LineCharts
fig = px.line(df, y = "sepal_width")
# pio.write_image(fig, 'output_graph.png')
pio.write_html(fig, 'output_file.html')
fig
fig = px.line(df, y = "sepal_width", line_dash="species", color="species")
pio.write_html(fig, 'output_file.html')
fig
Bar Chart
dfA = px.data.tips()
dfA
| total_bill | tip | sex | smoker | day | time | size | |
|---|---|---|---|---|---|---|---|
| 0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
| 1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
| 2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
| 3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
| 4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 239 | 29.03 | 5.92 | Male | No | Sat | Dinner | 3 |
| 240 | 27.18 | 2.00 | Female | Yes | Sat | Dinner | 2 |
| 241 | 22.67 | 2.00 | Male | Yes | Sat | Dinner | 2 |
| 242 | 17.82 | 1.75 | Male | No | Sat | Dinner | 2 |
| 243 | 18.78 | 3.00 | Female | No | Thur | Dinner | 2 |
244 rows × 7 columns
fig = px.bar(dfA, x = "day", y = "total_bill")
pio.write_html(fig, 'output_file.html')
fig
fig = px.bar(dfA, x = 'day', y = 'total_bill', color='sex')
pio.write_html(fig, 'output_file.html')
fig
fig = px.bar(dfA, 'day', 'total_bill', color='sex', facet_row='time')
pio.write_html(fig, 'output_file.html')
fig
fig = px.bar(dfA, 'day', 'total_bill', color='sex',facet_col='sex')
pio.write_html(fig, 'output_file.html')
fig
fig = px.bar(dfA, 'day', 'total_bill', color='sex', facet_row='time',facet_col='sex')
pio.write_html(fig, 'output_file.html')
fig
Scatter Plot
plotly.express.scatter(data_frame=None, x=None, y=None, color=None, symbol=None, size=None, hover_name=None, hover_data=None, facet_row=None, facet_col=None, facet_col_wrap=0, opacity=None, title=None, template=None, width=None, height=None, **kwargs)
fig = px.scatter(dfA, x = "total_bill", y = "tip", color="sex")
pio.write_html(fig, 'output_file.html')
fig
fig = px.scatter(dfA, x = "total_bill", y = "tip", color="sex", facet_col="sex")
pio.write_html(fig, 'output_file.html')
fig
fig = px.scatter(dfA, x = "total_bill", y = "tip", color="sex",facet_row="time", facet_col="sex")
pio.write_html(fig, 'output_file.html')
fig
fig = px.scatter(dfA, x = "total_bill", y = "tip", color="time", symbol="sex", size="tip")
pio.write_html(fig, 'output_file.html')
fig
fig = px.scatter(dfA, x = "total_bill", y = "tip", color="time", symbol="sex", size="size",
facet_row="day", facet_col="time")
pio.write_html(fig, 'output_file.html')
fig
Histogram
plotly.express.histogram(data_frame=None, x=None, y=None, color=None, facet_row=None, facet_col=None, barnorm=None, histnorm=None, nbins=None, title=None, template=None, width=None, height=None)
fig = px.histogram(dfA, x = "total_bill")
pio.write_html(fig, 'output_file.html')
fig
fig = px.histogram(dfA, "total_bill", color="sex", nbins=50, histnorm="percent", barmode="group")
pio.write_html(fig, 'output_file.html')
fig
#barmode can be ----- overlay, relative or group
px.histogram(dfA, x = "total_bill", y = "sex")
# pio.write_html(fig, 'output_file.html')
Pie Chart
plotly.express.pie(data_frame=None, names=None, values=None, color=None, color_discrete_sequence=None, color_discrete_map={}, hover_name=None, hover_data=None, custom_data=None, labels={}, title=None, template=None, width=None, height=None, opacity=None, hole=None)
px.pie(dfA, names="day")
px.pie(dfA, names="day", values="total_bill")
px.pie(dfA, names="day", values="total_bill", hole=0.5, opacity=0.8,
color_discrete_sequence=px.colors.sequential.RdBu)
Box Plot
plotly.express.box(data_frame=None, x=None, y=None, color=None, facet_row=None, facet_col=None, title=None, template=None, width=None, height=None, **kwargs)
px.box(dfA, x = "day", y = "tip")
px.box(dfA, x = "day", y = "tip", color="sex")
px.box(dfA, x = "day", y = "tip", color="sex", facet_row="time", notched=True)
px.box(dfA, x = "day", y = "tip", color="sex", facet_row="time", facet_col="sex",notched=True)
Violin Plots
px.violin(dfA, x = "day", y = "tip")
px.violin(dfA, x="day", y="tip", color="sex", facet_row="time")
3D Scatter Plots
plotly.express.scatter_3d(data_frame=None, x=None, y=None, z=None, color=None, symbol=None, size=None, range_x=None, range_y=None, range_z=None, title=None, template=None, width=None, height=None, **kwargs)
px.scatter_3d(dfA, x = "total_bill", y = "sex", z = "tip")
px.scatter_3d(dfA, x = "total_bill", y = "sex", z = "tip", size = "total_bill", color="day", symbol="time")
plot = gpx.Figure(data = [gpx.Scatter(
x = dfA['day'],
y = dfA['tip'],
mode = 'markers'
)])
plot.update_layout(
updatemenus = [
dict( buttons = list([
dict(
args = ["type", "scatter"],
label = "scatter plot",
method = "restyle"
),
dict(
args = ["type", "bar"],
label = "bar chart",
method = "restyle"
)
]),
direction = "down",
),
]
)